package demo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Game {
    private  static final String[] suits = {"红桃","梅花","方片","黑桃"};
    //买扑克
    public List<Poker> buyPoker(){

        List<Poker> pokers = new ArrayList<>();

        for (int i = 0; i < 4; i++) {   // 4种花色
            for (int j = 1; j <= 13; j++) { //每种花色13张牌
               /* String suit =suits[i];  // 花色
                int rank = j; // 牌数*/
                Poker poker = new Poker(suits[i],j);
                pokers.add(poker);
            }
        }
        return pokers;
    }
    //洗牌操作
    public void shuffle(List<Poker> pokers){
        // 以 i 为数量来生成随机数
        for (int i = pokers.size()-1; i > 0; i--) {
            Random random = new Random();
            int index = random.nextInt(i);
            swap(pokers,i,index);
        }
    }

    //交换牌
    private void swap(List<Poker> pokers , int i , int j){
        Poker tmp = pokers.get(i);
        pokers.set(i,pokers.get(j));
        pokers.set(j,tmp);
    }


    // 揭牌
    public List<List<Poker>> game(List<Poker> pokers){

        List<List<Poker>> hand = new ArrayList<>(); //定义一个二维数组 hand

        //先定义三个人的list
        List<Poker> hand1 = new ArrayList<>();
        List<Poker> hand2 = new ArrayList<>();
        List<Poker> hand3 = new ArrayList<>();

        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);

        for (int i = 0; i < 3; i++) {  // 这一层循环控制轮数， （表示一个人揭三张牌,在本代码情况中,玩的是比点）
            for (int j = 0; j < 3; j++) {  //三个人来揭牌
                Poker removePoker = pokers.remove(0);  //remove删除 ,一人揭一张当前的0下标,ArrayList的remove方法
                // 删除哪个下标的值会把哪个下标的值进行返回
                hand.get(j).add(removePoker); // j能代表人数，所以用j来代表hand的下标来揭removePoker这张牌
            }
        }
        return hand;
    }
}
